/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System.Windows;

namespace Xceed.Silverlight.ListBox
{
  public abstract class ListBoxContainerConfiguration<T> : ListBoxContainerConfigurationBase
    where T : ListBoxContainer
  {
    #region CONSTRUCTORS

    protected ListBoxContainerConfiguration()
      : base()
    {
    }

    protected ListBoxContainerConfiguration( ListBoxContainerConfiguration<T> template )
      : base( template )
    {
    }

    #endregion CONSTRUCTORS

    #region PROTECTED METHODS

    protected abstract T CreateContainerCore();

    protected virtual void PrepareContainerForItemCore( T container, object item )
    {
      // Apply the desired ContainerLayoutMode if different from ContainerLayoutMode.Default.
      ContainerLayoutMode configurationLayoutMode = this.LayoutMode;

      if( configurationLayoutMode != ContainerLayoutMode.Default )
      {
        // This ContainerLayoutMode is not used to position the container within the Panel/Active View.
        // It is only used to drive the container's visual states in order to offer a way of changing
        // the container's template dynamically.
        // It is pre-emptively set in the ListBox's PrepareContainerForItem method to the one used
        // to position the container by the Panel/ActiveView.
        ListBoxContainer.SetLayoutMode( container, configurationLayoutMode );
      }

      // Apply the container style.
      Style itemContainerStyle = this.ContainerStyle;

      if( itemContainerStyle != null )
      {
        container.Style = itemContainerStyle;
      }
      else
      {
        container.ClearValue( ListBoxContainer.StyleProperty );
      }

      // Apply the container content template.
      DataTemplate itemTemplate = this.ContentTemplate;

      if( itemTemplate != null )
      {
        container.ContentTemplate = itemTemplate;
      }
      else
      {
        // Clear the value so that the default object's DataTemplate is used.
        container.ClearValue( ListBoxContainer.ContentTemplateProperty );
      }
    }

    #endregion PROTECTED METHODS

    #region INTERNAL METHODS

    internal override ListBoxContainer CreateContainer()
    {
      return this.CreateContainerCore();
    }

    internal override void PrepareContainerForItem( ListBoxContainer container, object item )
    {
      // Set on the container a reference to the configuration that was used to prepare it.
      container.Configuration = this;

      this.PrepareContainerForItemCore( container as T, item );
    }

    #endregion INTERNAL METHODS
  }
}
